package com.hope.onlinestudy.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.text.TextUtils
import android.util.Log
import android.webkit.CookieManager
import android.webkit.CookieSyncManager
import com.google.gson.Gson
import com.hope.onlinestudy.BuildConfig
import java.io.File

object Utils {

    /**
     * 将cookie同步到WebView
     * @param url WebView要加载的url
     * @param cookie 要同步的cookie
     * @return true 同步cookie成功，false同步cookie失败
     */
    fun syncCookie(context: Context, url: String, cookie: String): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.createInstance(context)
        }
        val cookieManager = CookieManager.getInstance()
        cookieManager.setCookie(url, cookie)//如果没有特殊需求，这里只需要将session id以"key=value"形式作为cookie即可
        return !TextUtils.isEmpty(cookieManager.getCookie(url))
    }

    fun logs(cls: Class<*>, msg: String) {
        logs(cls.simpleName, msg)
    }

    fun logs(tag: String = "", msg: String) {
        if (BuildConfig.LOG_DEBUG)
            Log.e(tag, msg)
    }

    inline fun <reified T : Any> parserJson(data: String): T {
        return Gson().fromJson(data, T::class.java)
    }


    /**
     * Get package version.
     *
     * @return package version
     */
    fun getPackageVer(context: Context): String {
        var sPackage = "1.0.0"
        try {
            val pi = context.packageManager.getPackageInfo(context.packageName, 0)
            sPackage = pi.versionName
        } catch (ex: Exception) {
            ex.printStackTrace()
        }

        return sPackage
    }

    fun install(context: Context, file: File) {
        val installintent = Intent()
        installintent.action = Intent.ACTION_VIEW
        installintent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        installintent.setDataAndType(
            Uri.fromFile(file),
            "application/vnd.android.package-archive"
        )
        context.startActivity(installintent)
    }

    fun installNormal(context: Context, apkFile: File) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val apkUri = FileProvider.getUriForFile(context, "${BuildConfig.APPLICATION_ID}.fileProvider", apkFile)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
        } else {
            intent.setDataAndType(
                Uri.fromFile(apkFile),
                "application/vnd.android.package-archive"
            )
        }
        context.startActivity(intent)
    }

    fun getPackageCode(context: Context): Int {
        var sPackage = 0
        try {
            val pi = context.packageManager.getPackageInfo(context.packageName, 0)
            sPackage = pi.versionCode
        } catch (ex: Exception) {
            ex.printStackTrace()
        }

        return sPackage
    }

    fun isMobileNO(mobiles: String): Boolean {
        val telRegex = "[1][3456789]\\d{9}"
        return if (TextUtils.isEmpty(mobiles))
            false
        else
            mobiles.matches(telRegex.toRegex())
    }
}
